//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    internal class Table : IOfficeXmlSerializable
    {
        private const string xmlName = "tbl";
        private TableGrid _tblGrid;
        private TableProperties _tblPr;
        private readonly List<TableRow> _tr = new List<TableRow>();

        #region IOfficeXmlSerializable Members

        /// <summary>
        /// Generates an object from its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read(xmlName))
            {
                _tblPr = c.ReadOptionalElement<TableProperties>(TableProperties.xmlName);
                _tblGrid = c.ReadElement<TableGrid>();
                _tr.Clear();
                _tr.AddRange(c.ReadOptionalWhile<TableRow>(TableRow.xmlName));
            }
        }

        /// <summary>
        /// Convert an object into its XML representation
        /// </summary>
        /// <param name="context"></param>
        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                c.WriteOptionalElement( _tblPr );
                c.WriteElement(_tblGrid );
                c.WriteElements(_tr);
            }
        }

        #endregion
    }
}